package com.yuanfeng.basedata.repository;

import com.yuanfeng.basedata.entiry.KcsjUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName UserRepository
 * @Description T0D0
 * @Author yuanfeng
 * @Date 2019/9/6 21:30
 * @Version 1.0
 **/
@Repository
public interface UserRepository extends JpaRepository<KcsjUser, Integer> {

    /**
     * @return
     * @Description:查询所有用户
     * @Param
     * @date: 2019/9/6 23:57
     */

    List<KcsjUser> findAll();

    /**
     * @return
     * @Description: 根据ID查询
     * @Param
     * @date: 2019/9/7 9:23
     */

    @Override
    Optional<KcsjUser> findById(Integer integer);

    /**
     * @return 得到用户
     * @Description: 根据用户名密码查询用户
     * @Param userName 用户名
     * @Param userPassword 密码
     * @date: 2019/9/6 23:59
     */

    KcsjUser findKcsjUserByUserNameAndUserPassword(String userName, String userPassword);

    /**
     * @return
     * @Description:保存用户，即注册
     * @Param
     * @date: 2019/9/7 0:21
     */

    @Override
    <S extends KcsjUser> S save(S s);

    /**
     * @return 返回影响行数
     * @Description: 根据用户ID修改该用户信息
     * @Param
     * @date: 2019/9/7 8:27
     */

    @Modifying
    @Query(value = "update kcsj_user set kcsj_user_name = :userName,kcsj_user_password = :userPassword,kcsj_user_email = :userEmail,kcsj_user_birthday =:userBirthday," +
            "kcsj_user_address = :userAddress where kcsj_user_id = :userId", nativeQuery = true)
    int updateUserInfoById(@Param("userName") String userName,
                           @Param("userPassword") String userPassword,
                           @Param("userEmail") String userEmail,
                           @Param("userBirthday") Date userBirthday,
                           @Param("userAddress") String userAddress,
                           @Param("userId") Integer userId);


    /**
     * @return 返回新增得用户
     * @Description: 根据ID删除用户
     * @Param 用户实体
     * @date: 2019/9/7 0:23
     */

    KcsjUser deleteByUserId(Integer userId);


}
